package com.voltmao.service.application.corp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.voltmao.persistent.mapper.CorpEmployesUserMapper;
import com.voltmao.persistent.mapper.CorpOrganizationMapper;
import com.voltmao.persistent.model.CorpEmployesUser;
import com.voltmao.persistent.model.CorpEmployesUserCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.ParamsUtil;
import com.voltmao.webpro.utils.PropertiesUtils;
import com.voltmao.webpro.utils.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* 员工管理ServiceImpl
* @author zya
* 2018/04/04
*
* */
@Service
public class EmployesUserServiceImpl {
    @Autowired
    CorpEmployesUserMapper corpEmployesUserMapper;

    @Autowired
    CorpOrganizationMapper corpOrganizationMapper;

    /**
    * 根据条件查询员工信息
    *
    * */
    public DataPage getEmployerPageData(Map<String, Object> params){
        //分页对象
        DataPage dataPage = new DataPage();

        int pageIdx = StringUtils.parseInt(params.get("pageNumber") + "", 1);
        int pageSize = StringUtils.parseInt(params.get("pageSize") + "", 20);


        CorpEmployesUserCriteria example = new CorpEmployesUserCriteria();
        example.setPageSize(pageSize);
        example.setLimitStart((pageIdx - 1) * pageSize);

        if(params !=null && params.size() > 0){
            CorpEmployesUserCriteria.Criteria c = example.createCriteria();
            if(StringUtils.isNotEmpty(params.get("dataId"))){
                c.andDataIdEqualTo("u.",(String) params.get("dataId"));
            }
            //部门id
            if(StringUtils.isNotEmpty(params.get("orgId"))){
                c.andForCorpOrganizationEqualTo((String) params.get("orgId"));
            }
            //状态查询
            if(StringUtils.isNotEmpty(params.get("isDel"))){
                c.andIsDelEqualTo("u.",Integer.parseInt(params.get("isDel")+""));
            }
            //姓名查询
            if(StringUtils.isNotEmpty(params.get("name"))){
                c.andNameLike("u.","%" + params.get("name") + "%");
            }
        }

        dataPage.pageindex = pageIdx;
        dataPage.pagesize = pageSize;
        dataPage.rows = corpEmployesUserMapper.selectByExampleWithOrg(example);
        dataPage.total = corpEmployesUserMapper.countByExampleWithOrg(example);
        return dataPage;
    }

    /**
     * 根据渠道商id获取员工信息
     *
     * */
    public CorpEmployesUser getUserByProviderId(String id){
        return corpEmployesUserMapper.getUserByProviderId(id);
    }

    /**
    * 根据id获取员工信息
    *
    * */
    public CorpEmployesUser selectById(String id){
        return corpEmployesUserMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增或更新员工信息
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdate(Map<String,Object> params) {
        //检查参数完整性
        String [] flag = {"flag"};
        ParamsUtil.checkNotNull(params,flag);
        String tmpFlag = params.get("flag").toString();
        //新增
        if(tmpFlag.equals("INSERT")){
            String[] checkParams = {"name","phone_no","position","for_corp_organization"};
            ParamsUtil.checkNotNull(params,checkParams);
        }
        //更新
        if(tmpFlag.equals("UPDATE")){
            String[] checkParams = {"dataId"};
            ParamsUtil.checkNotNull(params,checkParams);
        }
        //删除
        if(tmpFlag.equals("DELETE")){
            String[] checkParams = {"dataId","isDel"};
            ParamsUtil.checkNotNull(params,checkParams);
        }
        CorpEmployesUser corpEmpl = (CorpEmployesUser) PropertiesUtils.mapToObject(params,CorpEmployesUser.class);
        if (StringUtils.isEmpty(corpEmpl.getDataId())) {
            corpEmpl.setCreateDate(new Date());
            corpEmpl.setUpdateDate(new Date());
            //默认启用
            corpEmpl.setIsDel(0);
            corpEmpl.setDataId(UUID.randomUUID()+"");

            return corpEmployesUserMapper.insert(corpEmpl);
        } else {
            corpEmpl.setUpdateDate(new Date());
            return corpEmployesUserMapper.updateByPrimaryKeySelective(corpEmpl);
        }
    }

    /**
    * 根据部门id统计人数
    * */
    public int countOrgEmplNum(String orgId){
        return corpEmployesUserMapper.CountOrgEmplNum(orgId);
    }


    public List<Map<String, Object>> getBusinessPersonnel(){
        return corpEmployesUserMapper.getBusinessPersonnel();
    }

}
